package com.bluexmicro.example.page.pick.peripheral.scan

import android.bluetooth.BluetoothDevice
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.bluexmicro.example.page.pick.peripheral.PeripheralViewModel
import com.bluexmicro.example.R


@Composable
fun PeripheralListView(
    peripheralViewModel: PeripheralViewModel,
    onSelectedListener: (device: BluetoothDevice) -> Unit
) {
    val context = LocalContext.current
    DisposableEffect(Unit) {
        peripheralViewModel.startScan(context)
        onDispose {
            peripheralViewModel.stopScan()
        }
    }
    val items = remember {
        peripheralViewModel.deviceModels
    }
    Column {
        LazyColumn(modifier = Modifier.fillMaxWidth().weight(1f)) {
            itemsIndexed(items = items) { _, item ->
                PeripheralItemView(item, onSelectedListener)
                Divider(color = Color.LightGray, thickness = Dp.Hairline)
            }
        }
        Button(
            onClick = { peripheralViewModel.sortDevices() },
            modifier = Modifier.fillMaxWidth().defaultMinSize(minHeight = 64.dp)
                .padding(horizontal = 16.dp, vertical = 8.dp)
        ) {
            Text("过滤并排序")
        }
    }

}

@Composable
fun PeripheralItemView(
    model: PeripheralModel,
    onSelectedListener: (device: BluetoothDevice) -> Unit
) {
    val rssi by remember {
        model.rssi
    }
    val level by remember {
        model.rssiLevel
    }
    val name by remember {
        model.displayName
    }
    TextButton(onClick = {
        onSelectedListener.invoke(model.device)
    }, shape = RoundedCornerShape(0.dp)) {
        Column {
            Text(
                name,
                color = MaterialTheme.colorScheme.primary,
                style = MaterialTheme.typography.titleMedium
            )
            Text(
                model.macAddress,
                color = MaterialTheme.colorScheme.secondary,
                style = MaterialTheme.typography.bodyMedium
            )
        }
        Spacer(modifier = Modifier.weight(1f))
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.width(48.dp)
        ) {
            SignalIcon(level)
            Text("$rssi", style = MaterialTheme.typography.labelSmall)
        }
    }
}

@Composable
fun SignalIcon(level: Int) {
    val res = when (level) {
        0 -> R.drawable.ic_rssi_4
        1 -> R.drawable.ic_rssi_3
        2 -> R.drawable.ic_rssi_2
        3 -> R.drawable.ic_rssi_1
        else -> R.drawable.ic_rssi_1
    }
    Icon(
        painter = painterResource(res),
        null,
        modifier = Modifier.size(24.dp),
        tint = Color.Unspecified
    )
}